WireIt.ScriptContainer = function(options, layer){
	WireIt.ScriptContainer.superclass.constructor.call(this, options, layer);
   this.buildTextArea(options.scriptText || "function sampleScript (a,b,c) {\n\n  return a+b+c;\n}");
   
   this.createTerminals();
   
   // Reposition the terminals when the Workflow box is being resized
   this.ddResize.eventResize.subscribe(function(e, args) {
      this.positionTerminals();
      YAHOO.util.Dom.setStyle(this.textarea, "height", (args[0][1]-70)+"px");
   }, this, true);
	
}

YAHOO.lang.extend(WireIt.ScriptContainer, WireIt.WebUIContainer, {
  
   
   /**
    * Create the textarea for the javascript code
    * @method buildTextArea
    * @Input {String} codeText
    */
   buildTextArea: function(codeText) {

      this.textarea = WireIt.cn('textarea', null, {width: "90%", height: "70px", border: "0", padding: "5px"}, codeText);
      this.setBody(this.textarea);

      YAHOO.util.Event.addListener(this.textarea, 'change',this.createTerminals, this, true);
	  
      
   },
   
   /**
    * Create (and re-create) the terminals with this.nInputs input terminals
    * @method createTerminals
    */
   createTerminals: function() {  
	if (this.inputs) {
		this.oldInputs = this.inputs;
	}
	else {
		this.oldInputs = new Array;
	}

//read input terminals from textarea
      var match = (this.textarea.value.replace(new RegExp("[\\s]*","g"),'')).match((/^[\s\S]*function[\w\.]*\(([a-zA-Z0-9,]*)\){/));
	  this.name = (this.textarea.value.replace(new RegExp("[\\s]*","g"),'')).match((/^[\s\S]*function([\w\.]*)\(/))[1];
	  this.setName(this.name);	  
	  var sInputList = match ? match[1] : "";
      this.inputs = sInputList.split(','); // array of input terminals
      //alert("inputs: "+this.inputs);
	  var nInputs = (sInputList=="") ? 0 : this.inputs.length;
	  
	  if (this.inputs.toString() != this.oldInputs.toString()) {
		this.removeAllTerminals();	  
		// create input terminals
		      for(var i = 0 ; i < nInputs ; i++) {
				 var term = this.addTerminal({
					xtype: "WireIt.util.WebUITerminalInput",
					"name": this.inputs[i].replace(new RegExp("[\\n\\s]*","g"),''),
					wireConfig:{"color":"rgb(240,191,186)"},
					"ddConfig": {
						"type": "input:script",
						"allowedTypes":["output:any"]
					},
				});
		      }		
		// create output terminal
			   this.outputTerminal = this.addTerminal({
								xtype: "WireIt.util.WebUITerminalOutput",
								"name": "out",
								wireConfig:{"color":"rgb(240,191,186)"},
								"ddConfig": {
									"type": "output:script",
									"allowedTypes":["input:any"]
								},
							});      
				//alert(this.outputTerminal.options.ddConfig.allowedTypes);
				this.nInputs=nInputs;
			  
		      if (this.terminals.length > 0) {
		        this.positionTerminals();
		      }
		
		      // Declare the new terminals to the drag'n drop handler (so the wires are moved around with the container)
		      this.dd.setTerminals(this.terminals);
		}
    },
   
   /**
    * Reposition the terminals
    * @method positionTerminals
    */
   positionTerminals: function() {
      var width = WireIt.getIntStyle(this.el, "width");
      //input intervals	  
	  if (this.nInputs > 0) {
		  var inputsInterval = Math.floor(width/(this.nInputs+1));
		  for(var i = 1 ; i < (this.terminals.length) ; i++) {
			 var term = this.terminals[i-1];
			 WireIt.sn(term.el, null, {position: "absolute", top: "-15px", left: (inputsInterval*(i)-15)+"px"});
		  }
	  }
      // Output terminal
      WireIt.sn(this.outputTerminal.el, null, {position: "absolute", bottom: "-15px", left: (Math.floor(width/2)-15)+"px"});
      for(var j = 0 ; j < this.outputTerminal.wires.length ; j++) {
         this.outputTerminal.wires[j].redraw();
      }

    },
   
   /**
    * Extend the getConfig to add the "codeText" property
    * @method getConfig
    */
   getConfig: function() {
      var obj = WireIt.ScriptContainer.superclass.getConfig.call(this);
	  obj.scriptText = this.textarea.value;
	  obj.name = this.name;
      return obj;
   },
   
   initType: function(){
   //a stub
   },
	
	setName: function (name){
		this.ddHandle.removeChild(this.title);
		this.title = WireIt.cn('span', null, null, name);
		this.ddHandle.appendChild(this.title);
		this.title == "Script";
	},
});